<?php
namespace ff\auth;

use ff\base\Application;
use ff\base\Controller;
use ff\helpers\TokenParse;
use ff\network\Request;

class TokenAuthController extends Controller
{
    public $userid, $nickname, $expiration, $pwcode;

    public function beforeAction()
    {
        $verifyResult = $this->auth();
        if (!is_null($verifyResult)) {
            return $verifyResult;
        }
        parent::beforeAction();
    }

    public function auth($callrunController = null, $vars = null)
    {
        if (is_null($vars)) {
            $vars = $this->request->vars;
        }
        if (!isset($vars['token'])) {
            return ['code' => -1005];
        }
        list($this->userid, $this->nickname, $this->expiration, $this->pwcode) = TokenParse::get($vars['token']);

        if (!$this->userid) {
            return ['code' => -1006];
        }

        if (!$this->expiration || $this->expiration < TIMESTAMP) {
            return ['code' => -1007];
        }
        if (!is_null($callrunController)) {
            $callrunController->userid = $this->userid;
            $callrunController->nickname = $this->nickname;
            $callrunController->expiration = $this->expiration;
            $callrunController->pwcode = $this->pwcode;
        }
        return null;
    }

}
